package gew.beans;

import gew.services.MainMenuService;
import gew.system.RequestParameter;
import gew.data.MenuItem;
import gew.services.DimensionValueService;
import gew.services.LocaleService;
import gew.system.Constants;
import gew.system.Labels;
import gew.utils.SqlUtils;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean(name = "mainMenuBean")
@RequestScoped
public class MainMenuBean {

    private final static String FILTER_TEXT = "[filterText]";

    @EJB
    private MainMenuService mainMenuService;

    @EJB
    private DimensionValueService dvs;

    private RequestParameter rp;

    @PostConstruct
    public void init() {
        rp = new RequestParameter((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(), dvs);
    }

    public String getTradeLabel() {
        return mainMenuService.getTradeHeader(rp.getLocale());
    }

    public String getTradeUrl() {
        return mainMenuService.getTradeHeaderUrl(rp.getLocale());
    }

    public MenuItem getTradeGroup(int index) {
        return mainMenuService.getTradeGroupMenuItem(rp.getLocale(), index);
    }

    public Iterable<MenuItem> getTradeGroups() {
        return mainMenuService.getTradeGroupMenuItems(rp.getLocale());
    }

    public List<MenuItem> getTradeItems(int index) {
        return mainMenuService.getTradeMenuItems(rp.getLocale(), index);
    }

    public String getIndicatorsLabel() {
        return mainMenuService.getIndicatorsHeader(rp.getLocale());
    }

    public String getIndicatorsUrl() {
        return mainMenuService.getIndicatorsHeaderUrl(rp.getLocale());
    }

    public MenuItem getIndicatorsGroup(int index) {
        return mainMenuService.getIndicatorsGroupMenuItem(rp.getLocale(), index);
    }

    public Iterable<MenuItem> getIndicatorsGroups() {
        return mainMenuService.getIndicatorsGroupMenuItems(rp.getLocale());
    }

    public List<MenuItem> getIndicatorsItems(int index) {
        return mainMenuService.getIndicatorsMenuItems(rp.getLocale(), index);
    }

    public String getIndustriesLabel() {
        return mainMenuService.getIndustriesHeader(rp.getLocale());
    }

    public String getIndustriesUrl() {
        return mainMenuService.getIndustriesHeaderUrl(rp.getLocale());
    }

    public MenuItem getIndustriesGroup(int index) {
        return mainMenuService.getIndustriesGroupMenuItem(rp.getLocale(), index);
    }

    public Iterable<MenuItem> getIndustriesGroups() {
        return mainMenuService.getIndustriesGroupMenuItems(rp.getLocale());
    }

    public List<MenuItem> getIndustriesItems(int index) {
        return mainMenuService.getIndustriesMenuItems(rp.getLocale(), index);
    }

    public String getUrlHomePage() {
        return mainMenuService.getUrlHomePage(rp);
    }

    public String getUrlAbout() {
        return SqlUtils.SLASH
                .concat(rp.getContextPath()).concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH).concat(Constants.SECTION_ABOUT);
    }

    public String getUrlTerms() {
        return SqlUtils.SLASH
                .concat(rp.getContextPath()).concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH).concat(Constants.SECTION_TERMS);
    }

    public String getContextPath() {
        return rp.getContextPath();
    }

    public String getLocaleUrlCode() {
        return rp.getLocaleUrlCode();
    }
    
    public String getSecondLocale() {
        return LocaleService.getSecondLocale(rp.getLocaleUrlCode());
    }
    
    public String getSecondLanguageUrl() {
        return getSwitchLanguageUrl(LocaleService.getSecondLocale(rp.getLocaleUrlCode()));
    }

    public String getSwitchLanguageUrl(String locale) {
        switch (rp.getSection()) {
            case Constants.SECTION_INDUSTRIES:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_INDUSTRIES)
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getIndicatorIds().isEmpty() ? "" : rp.getIndicatorUrlCode())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getCountryIds().isEmpty() ? "" : rp.getCountryUrlCode());
            case Constants.SECTION_INDICATORS:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_INDICATORS)
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getIndicatorIds().isEmpty() ? "" : rp.getIndicatorUrlCode())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getCountryIds().isEmpty() ? "" : rp.getCountryUrlCode());
            case Constants.SECTION_TRADE_SUMMARY:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_TRADE_SUMMARY)
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getCommodityGroupTypeIds().isEmpty() ? Labels.get_PARAM_DEFAULT_COMMODITY_GROUP_TYPE_CODE_VALUE(rp.getLocale()) : rp.getCommodityGroupTypeUrlCode())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getReporterIds().isEmpty() ? Labels.get_PARAM_DEFAULT_TRADE_SUMMARY_COUNTRY(rp.getLocale()) : rp.getReporterUrlCode());
            case Constants.SECTION_TRADE:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_TRADE)
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getReporterUrlCode())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getTradeFlowUrlCode())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getPartnerUrlCode())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getPeriodUrlCode())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getCommodityGroupTypeUrlCode())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getCommodityUrlCode());
            case Constants.SECTION_ABOUT:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_ABOUT);
            case Constants.SECTION_TERMS:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_TERMS);
            case Constants.SECTION_ERROR:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_ERROR);
            case Constants.SECTION_SEARCH:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_SEARCH)
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getFilterText())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getSearchSectionUrlCode());
            default:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_HOME);
        }
    }

    public String getSwitchLanguageLabelRu() {
        return Labels.get_UI_LNG_RU(rp.getLocale());
    }

    public String getSwitchLanguageLabelEn() {
        return Labels.get_UI_LNG_EN(rp.getLocale());
    }

    public String getUI_TITLE_TERMS() {
        return Labels.get_UI_TITLE_TERMS(rp.getLocale());
    }

    public String getUI_TITLE_MAIN() {
        return Labels.get_UI_TITLE_MAIN(rp.getLocale());
    }

    public String getUI_TITLE_ABOUT() {
        return Labels.get_UI_TITLE_ABOUT(rp.getLocale());
    }

    public String getUI_FOOTER_HOME() {
        return Labels.get_UI_FOOTER_HOME(rp.getLocale());
    }

    public String getUI_INFO_ERROR1() {
        return Labels.get_UI_INFO_ERROR1(rp.getLocale());
    }

    public String getUI_INFO_ERROR2() {
        return Labels.get_UI_INFO_ERROR2(rp.getLocale());
    }

    public String getUI_INFO_ERROR3() {
        return Labels.get_UI_INFO_ERROR3(rp.getLocale());
    }

    public String getSearchUrlTemplate() {
        StringBuilder buf = new StringBuilder();

        buf
                .append(rp.getContextPath())
                .append(SqlUtils.SLASH)
                .append(rp.getLocaleUrlCode())
                .append(SqlUtils.SLASH)
                .append(Constants.SECTION_SEARCH)
                .append(SqlUtils.SLASH)
                .append(FILTER_TEXT)
                .append(SqlUtils.SLASH);

        return buf.toString();
    }

    public String getFilterText() {
        return rp.getFilterText();
    }

    private String getUrl(String locale) {
        switch (rp.getSection()) {
            case Constants.SECTION_INDUSTRIES:
                return IndustriesBean.getUrl(
                        rp.getContextPath(),
                        locale,
                        rp.getIndicatorIds().isEmpty() ? SqlUtils.EMPTY : rp.getIndicatorUrlCode(),
                        rp.getCountryIds().isEmpty() ? SqlUtils.EMPTY : rp.getCountryUrlCode()
                );
            case Constants.SECTION_INDICATORS:
                return IndicatorsBean.getUrl(
                        rp.getContextPath(),
                        locale,
                        rp.getIndicatorIds().isEmpty() ? SqlUtils.EMPTY : rp.getIndicatorUrlCode(),
                        rp.getCountryIds().isEmpty() ? SqlUtils.EMPTY : rp.getCountryUrlCode()
                );
            case Constants.SECTION_TRADE_SUMMARY:
                return TradeSummaryBean.getUrl(
                        rp.getContextPath(),
                        locale,
                        rp.getCommodityGroupTypeIds().isEmpty() ? Labels.get_PARAM_DEFAULT_COMMODITY_GROUP_TYPE_CODE_VALUE(rp.getLocale()) : rp.getCommodityGroupTypeUrlCode(),
                        rp.getReporterIds().isEmpty() ? Labels.get_PARAM_DEFAULT_TRADE_SUMMARY_COUNTRY(rp.getLocale()) : rp.getReporterUrlCode()
                );
            case Constants.SECTION_TRADE:
                return TradeBean2.getUrl(
                        rp.getContextPath(),
                        locale,
                        rp.getReporterUrlCode(),
                        rp.getTradeFlowUrlCode(),
                        rp.getPartnerUrlCode(),
                        rp.getPeriodUrlCode(),
                        rp.getCommodityGroupTypeUrlCode(),
                        rp.getCommodityUrlCode()
                );
            case Constants.SECTION_ABOUT:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_ABOUT);
            case Constants.SECTION_TERMS:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_TERMS);
            case Constants.SECTION_ERROR:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_ERROR);
            case Constants.SECTION_SEARCH:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_SEARCH)
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getFilterText())
                        .concat(SqlUtils.SLASH)
                        .concat(rp.getSearchSectionUrlCode());
            default:
                return rp.getContextPath()
                        .concat(SqlUtils.SLASH)
                        .concat(locale)
                        .concat(SqlUtils.SLASH)
                        .concat(Constants.SECTION_HOME);
        }
    }

    public String getUrlEn() {
        return getUrl(LocaleService.EN);
    }

    public String getUrlRu() {
        return getUrl(LocaleService.RU);
    }

    public String getUrlCurrent() {
        return getUrl(rp.getLocaleUrlCode());
    }

    public String getHrefLangEn() {
        switch (rp.getLocaleUrlCode()) {
            case LocaleService.EN:
                return SqlUtils.X;
            default:
                return LocaleService.EN;

        }
    }

    public String getHrefLangRu() {
        switch (rp.getLocaleUrlCode()) {
            case LocaleService.RU:
                return SqlUtils.X;
            default:
                return LocaleService.RU;

        }
    }
    
    public String getUI_TITLE_RIGHTS1(){
        return Labels.get_UI_TITLE_RIGHTS1(rp.getLocale());
    }
    
    public String getUI_TITLE_RIGHTS2(){
        return Labels.get_UI_TITLE_RIGHTS2(rp.getLocale());
    }
    
    public String getUI_TITLE_RIGHTS3(){
        return Labels.get_UI_TITLE_RIGHTS3(rp.getLocale());
    }
    
    public String getUI_TITLE_RIGHTS4(){
        return Labels.get_UI_TITLE_RIGHTS4(rp.getLocale());
    }
    
    public String getMETA_OG_TITLE(){
        return Labels.get_META_OG_TITLE(rp.getLocale());
    }

    public String getMETA_OG_DESCRIPTION() {
        return Labels.get_META_OG_DESCRIPTION(rp.getLocale());
    }

    public String getUI_LINK_FB() {
        return Labels.get_UI_LINK_FB(rp.getLocale());
    }

    public String getUI_LINK_IN() {
        return Labels.get_UI_LINK_IN(rp.getLocale());
    }

    public String getUI_LINK_LJ() {
        return Labels.get_UI_LINK_LJ(rp.getLocale());
    }

    public String getUI_LINK_TW() {
        return Labels.get_UI_LINK_TW(rp.getLocale());
    }

    public String getHttpHeader() {
        return rp.getHttpHeader();
    }
}
